<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>Multimap constructors &amp; destructors</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Multimaps</a> &gt; <a href=
    "multimap_constructors.html">Multimap constructors &amp;
    destructors</a>
  </div>

  <div class="name-format">
    Multimap constructors &amp; destructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;map&gt;
  multimap();
  multimap( const multimap&amp; c );
  multimap( iterator begin, iterator end,
            const key_compare&amp; cmp = Compare(), const allocator&amp; alloc = Allocator() );
  ~multimap();
</pre>

  <p>Multimaps have several constructors:</p>

  <ul>
  <li>The default constructor takes no arguments, creates a new
  instance of that multimap, and runs in <a
  href="../complexity.html">constant time</a>.</li>
  <li>The default copy constructor runs in <a href=
  "../complexity.html">linear time</a> and can be used to create a new
  multimap that is a copy of the given multimap <em>c</em>.</li>
  <li>Multimaps can also be created from a range of elements defined by
  <em>begin</em> and <em>end</em>.  When using this constructor, an
  optional comparison function <em>cmp</em> and allocator
  <em>alloc</em> can also be provided.</li>
  </ul>

  <p>The default destructor is called when the multimap should be
  destroyed.</p>

  <p>The template definition of multimaps requires that both a key
  type and value type be supplied.  For example, you can instantiate a
  multimap that maps strings to integers with this statement:</p>

  <pre class="example-code">
  multimap&lt;string,int&gt; m;
</pre>

  <p>You can also supply a comparison function and an allocator in the
  template:</p>

  <pre class="example-code">
  multimap&lt;string,int,myComp,myAlloc&gt; m;
</pre>

  <p>For example, the following code uses a multimap to associate a
  series of employee names with numerical IDs:</p>

  <pre class="example-code">
  multimap&lt;string,int&gt; m;

  int employeeID = 0;
  m.insert( pair&lt;string,int&gt;("Bob Smith",employeeID++) );
  m.insert( pair&lt;string,int&gt;("Bob Thompson",employeeID++) );
  m.insert( pair&lt;string,int&gt;("Bob Smithey",employeeID++) );
  m.insert( pair&lt;string,int&gt;("Bob Smith",employeeID++) );

  cout &lt;&lt; "Number of employees named 'Bob Smith': " &lt;&lt; m.count("Bob Smith") &lt;&lt; endl;
  cout &lt;&lt; "Number of employees named 'Bob Thompson': " &lt;&lt; m.count("Bob Thompson") &lt;&lt; endl;
  cout &lt;&lt; "Number of employees named 'Bob Smithey': " &lt;&lt; m.count("Bob Smithey") &lt;&lt; endl;

  cout &lt;&lt; "Employee list: " &lt;&lt; endl;
  for( multimap&lt;string, int&gt;::iterator iter = m.begin(); iter != m.end(); ++iter ) {
    cout &lt;&lt; " Name: " &lt;&lt; iter-&gt;first &lt;&lt; ", ID #" &lt;&lt; iter-&gt;second &lt;&lt; endl;
  }
</pre>

  <p>When run, the above code produces the following output.  Note
  that the employee list is displayed in alphabetical order, because
  multimaps are sorted associative containers:</p>

  <pre class="example-code">
  Number of employees named 'Bob Smith': 2
  Number of employees named 'Bob Thompson': 1
  Number of employees named 'Bob Smithey': 1
  Employee list:
   Name: Bob Smith, ID #0
   Name: Bob Smith, ID #3
   Name: Bob Smithey, ID #2
   Name: Bob Thompson, ID #1
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="count.html">count</a><br>
    <a href="insert.html">insert</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
